package cn.edu.qzuie.artist.module.td.controller.admin.publishactivity;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.publishactivity.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.publishactivity.PublishActivityDO;
import cn.edu.qzuie.artist.module.td.service.publishactivity.PublishActivityService;

@Tag(name = "管理后台 -  发布活动")
@RestController
@RequestMapping("/td/publish-activity")
@Validated
public class PublishActivityController {

    @Resource
    private PublishActivityService publishActivityService;

    @PostMapping("/create")
    @Operation(summary = "创建 发布活动")
    @PreAuthorize("@ss.hasPermission('td:publish-activity:create')")
    public CommonResult<Long> createPublishActivity(@Valid @RequestBody PublishActivitySaveReqVO createReqVO) {
        return success(publishActivityService.createPublishActivity(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新 发布活动")
    @PreAuthorize("@ss.hasPermission('td:publish-activity:update')")
    public CommonResult<Boolean> updatePublishActivity(@Valid @RequestBody PublishActivitySaveReqVO updateReqVO) {
        publishActivityService.updatePublishActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除 发布活动")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:publish-activity:delete')")
    public CommonResult<Boolean> deletePublishActivity(@RequestParam("id") Long id) {
        publishActivityService.deletePublishActivity(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得 发布活动")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:publish-activity:query')")
    public CommonResult<PublishActivityRespVO> getPublishActivity(@RequestParam("id") Long id) {
        PublishActivityDO publishActivity = publishActivityService.getPublishActivity(id);
        return success(BeanUtils.toBean(publishActivity, PublishActivityRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得 发布活动分页")
    @PreAuthorize("@ss.hasPermission('td:publish-activity:query')")
    public CommonResult<PageResult<PublishActivityRespVO>> getPublishActivityPage(@Valid PublishActivityPageReqVO pageReqVO) {
        PageResult<PublishActivityDO> pageResult = publishActivityService.getPublishActivityPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PublishActivityRespVO.class));
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得 发布活动分页")
//    @PreAuthorize("@ss.hasPermission('td:publish-activity:query')")
//    public CommonResult<PageResult<PublishActivityPagesRespVO>> getPublishActivityPage(@Valid PublishActivityPageReqVO pageReqVO) {
//        PageResult<PublishActivityPagesRespVO> pageResult = publishActivityService.getPublishActivityPageWithName(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, PublishActivityPagesRespVO.class));
//    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出 发布活动 Excel")
    @PreAuthorize("@ss.hasPermission('td:publish-activity:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPublishActivityExcel(@Valid PublishActivityPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PublishActivityDO> list = publishActivityService.getPublishActivityPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, " 发布活动.xls", "数据", PublishActivityRespVO.class,
                        BeanUtils.toBean(list, PublishActivityRespVO.class));
    }

}